1
|
|
|
const requestify = require('requestify'); |
2
|
|
|
|
3
|
|
|
let getBaseUrl = (url) => { |
4
|
|
|
if (!url) { |
5
|
|
|
throw new Error('url not specified - can\'t get base url'); |
6
|
|
|
} |
7
|
|
|
let index = url.indexOf('rest/'); |
8
|
|
|
if (index === -1) { |
9
|
|
|
throw new Error('Invalid url (can\'t find \'rest/\' string) - can\'t get base url'); |
10
|
|
|
} |
11
|
|
|
let baseUrl = url.substr(0, index + 5); |
12
|
|
|
|
13
|
|
|
return baseUrl; |
14
|
|
|
}; |
15
|
|
|
|
16
|
|
|
let extractSessionCookie = (cookies, cookieName) => { |
17
|
|
|
if (!cookieName) { |
18
|
|
|
throw new Error('cookie name not specified - can\'t extract session cookie'); |
19
|
|
|
} |
20
|
|
|
if (!cookies) { |
21
|
|
|
throw new Error('cookies not specified - can\'t extract session cookie'); |
22
|
|
|
} |
23
|
|
|
if (!Array.isArray(cookies)) { |
24
|
|
|
throw new Error('cookies is not an array - can\'t extract session cookie'); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
try { |
28
|
|
|
var sessionCookie = cookies.find((cookie) => { // eslint-disable-line no-var |
29
|
|
|
let name = cookie.split('=')[0]; |
30
|
|
|
let value = cookie.split('=')[1].split(';')[0]; |
31
|
|
|
return ((name === cookieName) && (value != '""')); |
32
|
|
|
}).split(';')[0]; |
33
|
|
|
} catch (error) { |
34
|
|
|
throw new Error('cookie name not found in cookies - can\'t extract session cookie'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return sessionCookie; |
38
|
|
|
}; |
39
|
|
|
|
40
|
|
|
module.exports = { |
41
|
|
|
/** |
42
|
|
|
* Login to Jira is order to retrieve the session cookie |
43
|
|
|
* @param {String} userName the user name to use to log in Jira |
|
|
|
|
44
|
|
|
* @param {String} password the password of the user used to log in Jira |
|
|
|
|
45
|
|
|
* @param {String} url the url of the request to execute - it used to compute the login url |
|
|
|
|
46
|
|
|
* @return {Promise} Returns the sessions cookie as a {String} (<cookie name>=<cookie value>) |
47
|
|
|
*/ |
48
|
|
|
login: (userName, password, url) => new Promise((resolve, reject) => { |
49
|
|
|
try { |
50
|
|
|
let loginUrl = getBaseUrl(url) + 'auth/1/session'; |
51
|
|
|
let body = {'username': userName, 'password': password}; |
52
|
|
|
let options = { |
53
|
|
|
cache: { |
54
|
|
|
cache: false, |
55
|
|
|
}, |
56
|
|
|
}; |
57
|
|
|
|
58
|
|
|
requestify.post(loginUrl, body, options) |
59
|
|
|
.then((response) => { |
60
|
|
|
resolve(extractSessionCookie(response.headers['set-cookie'], JSON.parse(response.body).session.name)); |
61
|
|
|
}) |
62
|
|
|
.catch((error) => { |
63
|
|
|
reject(new Error('Can\'t get cookie [' + error.message + ']')); |
64
|
|
|
}); |
65
|
|
|
} catch (error) { |
66
|
|
|
reject(new Error('Can\'t get cookie [' + error.message + ']')); |
67
|
|
|
} |
68
|
|
|
}), |
69
|
|
|
/** |
70
|
|
|
* Builds the header to be added to requests using cookie based authentication |
71
|
|
|
* @param {String} sessionCookie the cookie: '<cookie name>=<cookie value>' |
72
|
|
|
* @return {Object} the header object |
73
|
|
|
*/ |
74
|
|
|
getHeader: (sessionCookie) => { |
75
|
|
|
return {'cookie': sessionCookie, 'Cache-Control': 'public, max-age=60'}; |
76
|
|
|
}, |
77
|
|
|
}; |
78
|
|
|
|